package com.xidian.zhiancloud.service.emergencyManagement;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.EmergencyMaterialsInventoryManagementMapper;
import com.xidian.zhiancloud.dao.WarehouseOutMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.EmergencyMaterialsInventoryManagement;
import com.xidian.zhiancloud.model.WarehouseOut;

import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.vo.emergencyManagement.WarehouseOutVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

@Service
public class WarehouseOutService extends ServiceImpl<WarehouseOutMapper, WarehouseOut> implements IService<WarehouseOut> {
    @Autowired
    private WarehouseOutMapper warehouseOutMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private EmergencyMaterialsInventoryManagementMapper emergencyMaterialsInventoryManagementMapper;

    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean add(WarehouseOut warehouseOut) {
        int result = warehouseOutMapper.insert(warehouseOut);
        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        Long numOut=warehouseOut.getQuantity();
        Long emergencyMaterialsId=warehouseOut.getEmergencyMaterialsId();
        EmergencyMaterialsInventoryManagement emergencyMaterialsInventoryManagement=emergencyMaterialsInventoryManagementMapper.selectById(emergencyMaterialsId);
        Long numE=emergencyMaterialsInventoryManagement.getNum();
        emergencyMaterialsInventoryManagement.setNum(numE-numOut);
        int i = emergencyMaterialsInventoryManagementMapper.updateById(emergencyMaterialsInventoryManagement);
        return i==1;
    }

    public List<WarehouseOutVO> getWarehouseOut(Long id) {
        LambdaQueryWrapper<WarehouseOut> queryWrapper= Wrappers.<WarehouseOut>lambdaQuery();
        queryWrapper.eq(WarehouseOut::getEmergencyMaterialsId,id);
        List<WarehouseOut> list=warehouseOutMapper.selectList(queryWrapper);
        List<WarehouseOutVO> ans=new LinkedList<>();
        list.forEach(t -> {
            WarehouseOutVO vo = new WarehouseOutVO();
            BeanUtils.copyProperties(t, vo);
            SysUser user=sysUserMapper.selectById(vo.getRecipientId());
            vo.setRecipientName(user==null?"该用户已删除":user.getName());
            ans.add(vo);
        });
        return ans;
    }
}
